﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using ExcelAutomator.Configuration;
using ExcelAutomator.Validation;
using NUnit.Framework;

namespace ExcelAutomator.Tests.IntegrationTests.RangeExecutors
{
	[TestFixture]
	public class RangeClearTestFixture : ExcelTestFixtureBase
	{
		#region FailValidationIfWorkSheetCannotBeFound
		[Test]
		public void FailValidationIfWorkSheetCannotBeFound()
		{
			var cfg = new ExcelAutomatorConfiguration()
			          	{
			          		Settings = new SettingsConfiguration(){Input = SourceWbName}, 
										ExecutionSets = new List<ExecutionSetConfiguration>()
										                	{
										                		new ExecutionSetConfiguration()
										                			{
										                				Id = "My ExecutionSet Id", 
																						Executors = new List<ExecutorConfigurationWrapper>()
																						            	{
																						            		new ExecutorConfigurationWrapper(
																															"range", 
																															Constants.ExecutorExtensionsNamespace, 
																															string.Format(@"<range action='clear' targetAddress='FooBar!A1' xmlns='{0}'/>", Constants.ExecutorExtensionsNamespace))
																						            	}
										                			}
										                	}
			          	};



			var sut = new ExcelAutomationService(cfg);

			ValidationReport report;
			var isValid = sut.Validate(out report);

			Assert.That(isValid, Is.False);
			Assert.That(report.Violations.First().IsValid, Is.False);
		}
		#endregion

		#region ClearTextRangeInTarget
		[Test]
		public void ClearTextRangeInTarget()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.ClearRange.xml"));
		  var sut = new ExcelAutomationService(cfg);

		  sut.ExecuteAutomation();

			base.StartExcel();
		  ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet1", "A1", a1=> Assert.That(a1, Is.Null));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet1", "A2", a2 => Assert.That(a2, Is.Null));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet1", "A3", a3 => Assert.That(a3, Is.Null));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet1", "A4", a4 => Assert.That(a4, Is.Null));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet1", "A5", a5 => Assert.That(a5, Is.Null));
		  base.QuitExcel();

		}
		#endregion

	}
}
